import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Home from '../views/home.vue'
import Customer from '../components/customer.vue'
import Manager from '../components/manager.vue'
import APIDoc from '../views/apiDoc'
Vue.use(VueRouter)

const routes = [
	{path: '/',redirect:'/login'},
	{path: '/login',name: 'login',component: Login},
	//访问后台时，自动加载上“用户子组件”
	{path: '/home',name: 'home',component: Home, redirect:"/customer" ,children:[
		{path: '/customer',name: 'customer',component: Customer},
		{path: '/manager',name: 'manager',component: Manager},
		{path:'/apidoc',name:'apidoc',components: {apidoc:APIDoc}}
	]}
	
 
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
	//to 表示要访问的路径
	//from 表示从哪个路径跳转而来
	//next 是一个函数，表示放行
	
	//如果访问登录页，直接放行
	if(to.path=="/login") return next()
	//访问的不是登录页，查看是否有token,没有返回登录页
	const tokenStr=window.sessionStorage.getItem("token")		
	if(!tokenStr){
		console.log("尚未登陆，自动跳转回首页")
		return next('/login')
	}
	next()
})

export default router
